﻿
Partial Class App_Categorize
    Inherits MyUI.PageBaseApp

    'Dim WithEvents d As New SimilText.Tools.Dictionary
    Dim FolderParentId As Integer

    Protected SortGridLocations As UI.SortControl
    Public CurrAccount As mDB.Account



    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load


        FolderParentId = Utils.Conv.ToInt(Me.HidSelectFolderId.Value) ' Me.HidSelectFolderId.Value

        SortGridLocations = New UI.SortControl(Me.GridUsers.ID, Me.ViewState, "Accuracy", SortDirection.Descending)

        CurrAccount = CurrUser.CurrAccount

        Me.Items("RootFolderID") = -1
        Me.Items("tree_type_id") = CInt(mDB.Folder.GetFolderType(mDB.Folder.DataType.CATEGORIZE, CurrAccount.Network_Id))

        If Not Me.IsPostBack Then
            SetDataPage()
        Else

        End If


    End Sub

    Public Sub SetDataPage()

        SetListAccountFollowQuery()
    End Sub

    '#Region "ACCOUNT"

    '    Protected Sub AccountSelect_SelectedChanged() Handles AccountSelect.SelectedChanged

    '        SetDataPage()

    '    End Sub

    '#End Region

#Region "CATEGORIZE"

    Public ParentFold As mDB.Folder

    Public Sub SetListQuery(AddRow As Boolean)
        Dim g = Me.GridElaborations
        'Dim ParentId = Me.HidSelectFolderId.Value
        Dim dt As DataTable

        If Me.FolderParentId = 0 Then Exit Sub

        'QUERY
        ParentFold = DB.TAB.Folder.GetByPK(Me.FolderParentId)

        If ParentFold.User_Id = -1 Then
            dt = DB.TAB.Categorize.
                REL.Categorize.Folder.
                BY.Folder.Ref_Id(Me.FolderParentId).
                BY.Folder.User_Id(-1).
            ToDataTable

            'BY.Folder.User_Id(-1, mDB.Folder.GetFolderType(mDB.Folder.FuncType.CATEGORIZE, CurrAccount.Network_Id)).

        Else
            'dt = CurrUser.Tab_Query(ParentId).ToDataTable
            dt = CurrUser.Tab_Categorize(Me.FolderParentId).ToDataTable

        End If

        'categori selezionate
        Dim QueryFoll = CurrAccount.Tab_Categorize_Follower.ToDictionary(Of Integer)("categorize_id")

        'ADD
        If AddRow Then
            Utils.DT.AddDataRowAt(dt, Me.GridElaborations.Rows(0).DataItemIndex)
            Me.GridElaborations.EditIndex = 0
            Me.GridElaborations.SelectedIndex = 0

        End If

        '-------------------------------------------------------
        ' LAYOUT
        '-------------------------------------------------------

        UI.Grid.Bind(g, dt, {"id"})

        g.Columns(3).Visible = ParentFold.User_Id > 0

        For Each gr As GridViewRow In g.Rows
            Dim query = dt(UI.GridRow.GetIndexData(gr))
            Dim Follow = UI.GridRow.GetControl(Of ImageButton)(gr, "IbtFollow")
            'Dim UnFollow = UI.GridRow.GetControl(Of ImageButton)(gr, "IbtUnFollow")
            If Follow IsNot Nothing Then
                Follow.Visible = Not QueryFoll.ContainsKey(query("Id"))
            End If
            'UnFollow.Visible = Not Follow.Visible

        Next

        Me.UpPElaborations.Update()

    End Sub
    Protected Sub GridElaborations_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridElaborations.PageIndexChanging

        GridElaborations.PageIndex = e.NewPageIndex

        SetListQuery(False)

    End Sub
    Protected Sub GridElaborations_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridElaborations.RowCommand
        Dim g As GridView = sender

        Dim id = UI.Grid.GetKey(sender, e.CommandSource)

        If id Is Nothing Then
            Exit Sub
        End If

        Dim gr = UI.GridRow.GetRow(e.CommandSource)

        Dim query = DB.TAB.Categorize.GetByPK(id)

        Select Case e.CommandName
            Case "EDIT_ROW"
                g.EditIndex = gr.RowIndex
                SetListQuery(False)

            Case "CANCEL_ROW_EDIT"

                g.EditIndex = -1
                SetListQuery(False)

            Case "UPDATE_ROW"

                If query Is Nothing Then
                    query = (New mDB.Categorize With {.Folder_Id = Me.FolderParentId}) '.SetDefault
                    DB.TAB.Categorize.InsertOnSubmit(query)
                End If

                query.Query = UI.GridRow.GetTextBox(gr, "TxtQuery").Text
                'query.Place = UI.GridRow.GetTextBox(gr, "TxtPlace").Text
                'query.Lang_Code = UI.GridRow.GetTextBox(gr, "TxtLangCode").Text
                query.Description = UI.GridRow.GetTextBox(gr, "TxtDescription").Text

                DB.SubmitChanges()

                g.EditIndex = -1
                SetListQuery(False)

            Case "DELETE_ROW"
                DB.TAB.Categorize.DeleteOnSubmit(query)
                DB.SubmitChanges()
                SetListQuery(False)
            Case "LOAD"

                g.SelectedIndex = gr.RowIndex


                Me.Txtpost.Text = query.Query

                Me.UpPtext.Update()

            Case "FOLLOW"

                Dim QueryFoll = CurrAccount.Tab_Categorize_Follower(query.Id).ToObj

                If QueryFoll Is Nothing Then
                    QueryFoll = New mDB.Categorize_Follower With {
                        .Account_Id = CurrAccount.Id, .Categorize_Id = query.Id, .Accuracy_Total_Account = 1000
                    }
                    DB.TAB.Categorize_Follower.InsertOnSubmit(QueryFoll)
                    DB.SubmitChanges()
                End If

                Me.SetListQuery(False)
                Me.SetListAccountFollowQuery()

        End Select

    End Sub
    Protected Sub BtnReload_UpPElaborations_Click(sender As Object, e As System.EventArgs) Handles BtnReload_UpPElaborations.Click

        SetListQuery(False)

    End Sub
    Protected Sub BtnSaveNew_Click(sender As Object, e As System.EventArgs) Handles BtnSaveNew.Click

        If Me.FolderParentId <> 0 Then
            Dim Cat = New mDB.Categorize With {
                .Description = Me.TxtDescription.Text, .Folder_Id = Me.FolderParentId, .Query = Me.Txtpost.Text}

            DB.TAB.Categorize.InsertOnSubmit(Cat)

            If Cat.SubmitChanges() Then
                LblMessCat.Text = "Categoria creata"
            Else
                LblMessCat.Text = "Categoria già esistente"
            End If

            SetListQuery(False)

        Else
            LblMessCat.Text = "Selzionare una cartella"

            'LblMessCat.Text = ""

        End If

    End Sub

#End Region

#Region "CATEGORIZE FOLLOWER"

    Public Sub SetListAccountFollowQuery()
        Dim g = Me.GridFollowQuery


        Dim dt = CurrAccount.Tab_Categorize_Follower.
            REL.Categorize_Follower.Categorize.
        ToDataTable()

        '            SetSelect("categorize.id", "query", "description", "accuracy_total_account").

        g.PageSize = 30
        UI.Grid.Bind(g, dt, {"id"})

        Me.UpPFollowQuery.Update()

    End Sub

    Protected Sub GridFollowQuery_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridFollowQuery.RowCommand
        Dim g As GridView = sender

        Dim id = UI.Grid.GetKey(g, e.CommandSource)

        If id Is Nothing Then
            Exit Sub
        End If

        Dim gr = UI.GridRow.GetRow(e.CommandSource)

        Dim query = DB.TAB.Categorize.GetByPK(id)


        Select Case e.CommandName

            Case "UNFOLLOW"

                CurrAccount.Tab_Categorize_Follower(query.Id).Delete.ExecuteCommand()
                Me.SetListQuery(False)
                Me.SetListAccountFollowQuery()

            Case "CATEGORIZE"

                Dim FollCat = CurrAccount.Tab_Categorize_Follower(query.Id).ToObj

                MyApp.Task.Elaborate.CategorizeFollowersAccountCategory(FollCat)

                Me.SetListAccountFollowQuery()




        End Select
    End Sub
    Protected Sub GridFollowQuery_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridFollowQuery.PageIndexChanging

        GridFollowQuery.PageIndex = e.NewPageIndex

        Me.SetListAccountFollowQuery()
    End Sub

    Protected Sub BtnUpdateFollQuery_Click(sender As Object, e As System.EventArgs) Handles BtnUpdateFollQuery.Click
        Dim g = Me.GridFollowQuery

        Dim RecEditID = (From k In g.DataKeys Select k.valUe).ToList
        If RecEditID.Count > 0 Then

            Dim Records = CurrAccount.Tab_Categorize_Follower.Where("categorize_Id", "in", RecEditID).ToDictionary(Of Integer)("categorize_Id")
            For ct As Integer = 0 To g.Rows.Count - 1

                Records(g.DataKeys(ct).Value).Accuracy_Total_Account = Utils.Conv.ToDouble(UI.Grid.GetTextBox(g, ct, "TxtAccuracyTotalAccount").Text)

            Next

            DB.SubmitChanges()

        End If

        Me.SetListAccountFollowQuery()


    End Sub


#End Region

#Region "ANALISYS"

    Public Sub SetDataSearch()

        '----------------------------------------------------------
        'RICERCA
        '----------------------------------------------------------
        Dim AD = mDB.Archive_Simil_Text.GetArchive(mDB.Archive_Simil_Text.ArchiveType.POST_FOLLOWERS, CurrAccount.Id).GetSimilTextArchiveData

        AD.Rank_word_word = Utils.Conv.ToInt(Me.TxtWordWord.Text)
        AD.Rank_Hashtag_hashtag = Utils.Conv.ToInt(Me.Txthashhash.Text)
        AD.Rank_word_hashtag = Utils.Conv.ToInt(Me.TxtWordhash.Text)
        AD.Rank_Hashtag_word = Utils.Conv.ToInt(Me.TxthashWord.Text)
        AD.Rank_stemming = Utils.Conv.ToInt(Me.Txtstemm.Text)
        AD.Rank_word_prox = Utils.Conv.ToInt(Me.Txtprox.Text)

        Dim f As New SimilText.FindSimilarText(AD)

        f.Text = Me.Txtpost.Text
        f.RankMin = Utils.Conv.ToInt(Me.TxtAcc.Text)
        f.Find()
        f.Close()

        Me.LblExec.Text = String.Format("Analisi effettuata in {0} sec.", f.Result.ExecTime)

        Dim t As Long
        t = Now.Ticks

        '----------------------------------------------------------
        ' TAG CLOUD
        '----------------------------------------------------------

        'sequeza ricerca
        Dim WordSearch = SimilText.TextTools.TextToWord(Me.Txtpost.Text).Distinct.ToDictionary(Of String)(Function(o) o)  'TextToNumber.ToDictionary(Of String)(Function(o) AD.Dictionary.WordsList(o).Word)
        Dim HashTagIds = f.Result.HashTagCluster1.OrderByDescending(Function(o) o.Value).Take(100).ToList
        Dim WordIds = f.Result.WordCluster1.OrderByDescending(Function(o) o.Value).Take(100).ToList

        Dim TotHashtag = f.Result.HashTagCluster1.Sum(Function(o) o.Value)
        Dim TotWord = f.Result.HashTagCluster1.Sum(Function(o) o.Value)
        Dim TotCloudHashtag = HashTagIds.Sum(Function(o) o.Value)
        Dim TotCloudword = WordIds.Sum(Function(o) o.Value)

        ' LAYOUT

        'hashtag
        UI.Rep.Bind(Me.RptHasttag, HashTagIds)
        For ct = 0 To RptHasttag.Items.Count - 1
            Dim d = HashTagIds(UI.Rep.GetIndexData(Me.RptHasttag, ct))
            Dim LinkButton = UI.Rep.GetControl(Of LinkButton)(Me.RptHasttag, ct, "LnkTag")
            Dim Word = AD.Dictionary.WordsList(d.Key).Word

            LinkButton.Text = Word
            LinkButton.CommandArgument = Word
            LinkButton.ToolTip = UI.Print.Num2(d.Value / TotCloudHashtag * 100) & "% sul cloud - " & UI.Print.Num2(d.Value / TotHashtag * 100) & "% sul tot"

            If WordSearch.ContainsKey(Word) Then
                LinkButton.ForeColor = Drawing.Color.Red
            End If
        Next

        'word
        UI.Rep.Bind(Me.RptWord, WordIds)
        For ct = 0 To RptWord.Items.Count - 1
            Dim d = WordIds(UI.Rep.GetIndexData(Me.RptWord, ct))
            Dim LinkButton = UI.Rep.GetControl(Of LinkButton)(Me.RptWord, ct, "LnkTag")
            Dim Word = AD.Dictionary.WordsList(d.Key).Word

            LinkButton.Text = Word
            LinkButton.CommandArgument = Word
            LinkButton.ToolTip = UI.Print.Num2(d.Value / TotCloudHashtag * 100) & "% sul cloud - " & UI.Print.Num2(d.Value / TotWord * 100) & "% sul tot"

            If WordSearch.ContainsKey(Word) Then
                LinkButton.ForeColor = Drawing.Color.Red
            End If
        Next

        Me.LblExec.Text &= SF("<br>tag in {0} sec.", (Now.Ticks - t) / 10000000)
        t = Now.Ticks

        '----------------------------------------------------------
        ' CLUSTER
        '----------------------------------------------------------

        ' 2
        Dim Cluster2 = f.Result.HashTagCluster2.OrderByDescending(Function(o) o.Value).Take(20).ToList

        UI.Rep.Bind(Me.RptCluster2, Cluster2)
        For ct = 0 To RptCluster2.Items.Count - 1
            Dim d = Cluster2(UI.Rep.GetIndexData(Me.RptCluster2, ct))
            Dim label = UI.Rep.GetControl(Of Label)(Me.RptCluster2, ct, "LblCluster")

            label.Text = Me.IdsToWord(d.Key, AD.Dictionary, WordSearch)
        Next

        Me.LblExec.Text &= SF("<br>cluster 2 in {0} sec.", (Now.Ticks - t) / 10000000)
        t = Now.Ticks

        ' 3
        Dim Cluster3 = f.Result.HashTagCluster3.OrderByDescending(Function(o) o.Value).Take(20).ToList

        UI.Rep.Bind(Me.RptCluster3, Cluster3)
        For ct = 0 To RptCluster3.Items.Count - 1
            Dim d = Cluster3(UI.Rep.GetIndexData(Me.RptCluster3, ct))
            Dim label = UI.Rep.GetControl(Of Label)(Me.RptCluster3, ct, "LblCluster")

            label.Text = Me.IdsToWord(d.Key, AD.Dictionary, WordSearch)
        Next


        Me.LblExec.Text &= SF("<br>cluster 3 in {0} sec.", (Now.Ticks - t) / 10000000)
        t = Now.Ticks


        '----------------------------------------------------------
        ' TROVA GLI ACCOUNT CON MAGGIOR ACCURACY
        '----------------------------------------------------------

        Dim Accounts = (
            From r In f.Result.Matches
            Group r By r.Attribute0 Into g = Group
            Select id = Attribute0, AccTot = g.Sum(Function(o) o.Accuracy), PostTotal = g.Count
            Where AccTot >= Utils.Conv.ToInt(Me.TxtAccUser.Text)
        ).ToList

        'elimino la lista precedende
        DB.TAB.Categorize_Account.BY.Categorize_Account.Account_Id(CurrAccount.Id, mDB.Categorize.Type.TEMP).Delete.ExecuteCommand()

        'inserisco gli account nel db
        DB.InitMultiRowsInsert()
        For Each Acc In Accounts
            DB.InsertRowOnSubmit(New mDB.Categorize_Account With {
                .Account_Id = CurrAccount.Id,
                .Categorize_Id = mDB.Categorize.Type.TEMP,
                .Account_Categozie_Id = Acc.id,
                .Post_Total = Acc.PostTotal,
                .Accuracy = Acc.AccTot
            })
        Next
        DB.SubmitChanges()

        Me.LblUserFound.Text = String.Format("Trovati {0} utenti", Accounts.Count)

        SetDataListAccount()

        Me.LblExec.Text &= SF("<br>account in {0} sec.", (Now.Ticks - t) / 10000000)
        t = Now.Ticks

        '----------------------------------------------------------
        ' POST PRENDE SOLO I PRIMI RISULTATI
        '----------------------------------------------------------
        'Dim Results = (From r In f.Result.Matches Take 50).ToList

        'Dim DtPosts As Object
        'If Results.Count = 0 Then
        '    DtPosts = Nothing
        'Else
        '    Dim PostIds = (From r In Results Select r.Id).ToList
        '    Dim Posts = DB.TAB.AccountPost.Where("id", "IN", PostIds).ToList
        '    DtPosts = (
        '        From r In Results
        '        Join p In Posts On p.Id Equals r.Id
        '        Select r.Accuracy, p.Id, p.Post_Text, r.Attribute0, r.Attribute1
        '    ).ToList

        'End If


        'Me.LblRes.Text = String.Format("Trovati {0} risultati", f.Result.CountResults)

        'UI.Grid.Bind(Me.GridResults, DtPosts)

        Me.LblExec.Text &= SF("<br>post in {0} sec.", (Now.Ticks - t) / 10000000)
        t = Now.Ticks

        ' Return f.Result.Text1

    End Sub
    Public Sub SetDataListAccount()

        'Dim List = CurrAccount.Tab_categorize(mDB.Categorize.Type.USER_ANALYSIS).ToObj
        'Dim List = CurrAccount.Tab_Categorize_Follower(mDB.Categorize_Follower.Type.TEMP).ToObj

        '        Dim UserList = DB.TAB.AccountList.BY.AccountList.UserId(CurrUser.Id, 1).ToObj

        Dim DtAcc = SortGridLocations.SortQuery(
            DB.TAB.Account.
            REL.Account.Account_Add_Info.
            REL.Account.Categorize_Account.
            BY.Categorize_Account.Account_Id(CurrAccount.Id, mDB.Categorize.Type.TEMP)
        ).
        ToDataTable()

        Me.GridUsers.PageSize = 30
        UI.Grid.Bind(Me.GridUsers, DtAcc)

    End Sub
    Public Function IdsToWord(s As String, Dic As SimilText.Dictionary, WordSearch As Dictionary(Of String, String)) As String

        Dim ids = s.Split({"_"}, StringSplitOptions.RemoveEmptyEntries)

        Dim w = ""

        For ct = 0 To ids.Length - 1
            If ct > 0 Then w &= " + "
            Dim word = Dic.WordsList(ids(ct)).Word
            If WordSearch.ContainsKey(word) Then
                w &= "<b>" & word & "</b>"
            Else
                w &= word
            End If

        Next

        Return w


    End Function
    'Private Sub Clustered(ids() As UInteger, n As Integer)


    '    Dim i = ids.Where(Function(o) dic.WordsList(o).IsHashtag).Distinct.OrderBy(Function(o) o).ToArray


    '    Select Case n
    '        Case 1
    '            For ct3 = 1 To i.Length
    '                IncKey({i(ct3 - 1)})
    '            Next
    '        Case 2
    '            For ct2 = 1 To i.Length - 1
    '                For ct3 = ct2 + 1 To i.Length
    '                    IncKey({i(ct2 - 1), i(ct3 - 1)})

    '                Next
    '            Next

    '        Case 3

    '            For ct1 = 1 To i.Length - 2
    '                For ct2 = ct1 + 1 To i.Length - 1
    '                    For ct3 = ct2 + 1 To i.Length
    '                        IncKey({i(ct1 - 1), i(ct2 - 1), i(ct3 - 1)})
    '                    Next
    '                Next
    '            Next

    '    End Select




    'End Sub

    'Dim Clusters As New Dictionary(Of String, Integer)
    'Private Sub IncKey(ids() As UInteger)

    '    Dim id = ""
    '    For ct = 0 To ids.Length - 1
    '        id &= ids(ct) & "_"
    '    Next

    '    If Clusters.ContainsKey(id) Then
    '        Clusters(id) += 1
    '    Else
    '        Clusters.Add(id, 1)

    '    End If



    'End Sub

    Protected Sub GridUsers_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridUsers.PageIndexChanging

        Me.GridUsers.PageIndex = e.NewPageIndex

        Me.SetDataListAccount()

    End Sub
    Protected Sub GridUsers_Sorting(sender As Object, e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridUsers.Sorting

        Me.SortGridLocations.Sort(e.SortExpression)

        Me.SetDataListAccount()


    End Sub

    Protected Sub Btncerca_Click(sender As Object, e As System.EventArgs) Handles Btncerca.Click

        SetDataSearch()

    End Sub

    Protected Sub RptHasttag_ItemCommand(source As Object, e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles RptHasttag.ItemCommand

        Me.Txtpost.Text &= " " & e.CommandArgument

        Me.UpPtext.Update()

    End Sub
#End Region



End Class
